(function() {
  
  function getScript(src) {
    document.write('<' + 'script src="' + src + '"' +
                   ' type="text/javascript"><' + '/script>');
  }
  getScript("http://maps.google.com/maps/api/js?sensor=false");
})();

function isMarkerVisible() {
   return (marker.getVisible());
}

function fillUrls() {
   var markerlat = null;
   var markerlng = null;
   if (isMarkerVisible()) {
      markerlat = marker.getPosition().lat();
      markerlng = marker.getPosition().lng();
   }
   var maptype = (map.getMapTypeId() == 'roadmap' ? 'map' : 'sat');
   fillUrlsCommon(map.getCenter().lat(), map.getCenter().lng(), map.getZoom(), maptype, markerlat, markerlng);
}

function processMarker(isGPS) {
   if (isGPS) {
      if (marker.getVisible()) {
         marker.setVisible(false);
      }
      gps_coords = new google.maps.LatLng(gps_lat, gps_lng);
      marker.setPosition(gps_coords);
      marker.setVisible(true);
      map.setCenter(gps_coords);
   } else {
      if (marker.getVisible()) {
         marker.setVisible(false);
      } else {
         marker.setPosition(map.getCenter());
         marker.setVisible(true);
      }
   }
   fillUrls();
   markerButtons(isGPS);
}

function initialize() {

   initMapCanvasSize();

   var lat = getParameter('lat');
   if (lat == null || isNaN(lat))
      lat = def_lat;
   var lng = getParameter('lng');
   if (lng == null || isNaN(lat))
      lng = def_lng;
   var z = parseInt(getParameter('z'));
   if (z == null || isNaN(z))
      z = def_zoom;

   var mapType = getParameter('t');
   if (mapType == null)
      mapType = google.maps.MapTypeId.ROADMAP;
   else 
      if (mapType == 'sat')
        mapType = google.maps.MapTypeId.HYBRID;
      else 
        mapType = google.maps.MapTypeId.ROADMAP;

   var mlat = getParameter('mlat');
   var mlng = getParameter('mlng');
   var markerCoordinates = null;
   if (mlat != null && !isNaN(mlat) && mlng != null && !isNaN(mlng)) {
      markerCoordinates = new google.maps.LatLng(mlat, mlng);
   }
   
   var mapCenter = new google.maps.LatLng(lat, lng);

   
   var myOptions = {
     zoom: z,
     center: mapCenter,
     mapTypeId: mapType,
     scaleControl: true,
     scaleControlOptions: {
         position: google.maps.ControlPosition.BOTTOM_LEFT
     },
     mapTypeControl: true,
     panControl: true,
     panControlOptions: {
        position: google.maps.ControlPosition.RIGHT_TOP,
     },
     zoomControl: true,
     zoomControlOptions: {
        position: google.maps.ControlPosition.LEFT_BOTTOM,
        style: google.maps.ZoomControlStyle.LARGE
     },
     rotateControl: true,
     rotateControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER,
     },
     streetViewControl: false,
   };
   
   map = new google.maps.Map(document.getElementById("map_canvas"),
       myOptions);


   google.maps.event.addDomListener(map, 'zoom_changed', fillUrls);
   google.maps.event.addDomListener(map, 'center_changed', fillUrls);
   google.maps.event.addDomListener(map, 'maptypeid_changed', fillUrls);


   // MARKER
   marker = new google.maps.Marker({
      map: map,
      draggable: true,
      animation: google.maps.Animation.DROP,
      position: markerCoordinates,
      visible: (markerCoordinates != null ? true : false)
    });
    google.maps.event.addListener(marker, 'position_changed', fillUrls);

    
    // init methods
    fillUrls();
    initMarkerButtons(marker.getVisible());
    pageOnLoad();
 }

function resizeMap() {
   google.maps.event.trigger(map, 'resize');
   fillUrls();
}
